package com.CMUBiS.Data;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.CMUBiS.DB.DBQuery;
import com.CMUBiS.DB.MySQLDBQuery;
import com.CMUBiS.PageControllers.PageController;
import com.CMUBiS.util.BlackHoleOutput;

/**
 * This class contains global variables that are used by all servlets
 * 
 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
 * @version 0.1
 * @since 0.1
 * @date 04/05/2011
 */
public class GlobalVars {
	private static final String EMPTY = "";
	
	// Controls if the HTML4 or HTML5 version of the website is used
	public static final boolean isHTML4 = true;
	
	// Controls whether debug statements are printed to sdtout
	public static final boolean debug = true;
	
	// Controls whether the pages are verbose when processing to stdout
	public static final boolean verbose = false;
	
	// A single instance of the DB class for all pages to use
	public static final DBQuery db = new MySQLDBQuery();
	
	// A black hole if we don't want the page to output
	public static final BlackHoleOutput blackHole = new BlackHoleOutput();
	
	// Controls if we print all errors at the bottom of pages
	public static final boolean printAllErrors = true;
	
	// Controls if we want to collect page statistics or not
	public static final boolean collectStats = true;
	
	public static final SimpleDateFormat sdfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat sdfMMYY = new SimpleDateFormat("MMyyyy");

	// Images location
	//public static final File localTempDir = new File("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\temp");
	//public static final File localImageDir = new File("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\img"); // Andy copy
	public static final File localTempDir = new File("/usr/share/tomcat7/webapps/temp");
	public static final File localImageDir = new File("/usr/share/tomcat7/webapps/img"); // Andy copy
	
	public static final String remoteImageDir = new String("/img/"); // don't put / or \ on this
	
	// IP addresses
	//public static final String imageTierIP = "http://127.0.0.1:8080";
	public static final String imageTierIP = "../";
	
	/**
	 * This method adds the page statistics to the counters. Allowing them to be accessed by the stats page
	 * TODO: Andy: make it add to stat counters
	 * 
	 * @param request
	 * @param out
	 * @param pageController
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static void addStats(HttpServletRequest request, PrintWriter out, PageController pageController){
		out.println("<BR />Statistics:");
		out.println("<br />" + request.getServerName());
		out.println("<br />Total Page Time: " + pageController.getTotalTime() + " ms");
		out.println("<br />Get Param Time: " + pageController.getParamTime() + " ms");
		out.println("<br />DB Time: " + pageController.getDBTime() + " ms");
		out.println("<br />Processing Time: " + pageController.getProcessingTime() + " ms");
		out.println("<br />Render Time: " + (pageController.getTotalTime()-(pageController.getParamTime()+pageController.getDBTime()+ pageController.getProcessingTime())) + " ms");
	}
	
	public static String makeLabel(String tag, int i){
		if(i >= 0)
			return "<label for=\"" + tag + i + "\">";
		else
			return "<label for=\"" + tag + "\">";
	}
}
